﻿using System;
using System.ComponentModel;
using LFSRecord2.Misc;
using LFSRecord2.Model.Media;
using LFSRecord2.Model.Sound;
using LFSRecord2.Model.Undo;

namespace LFSRecord2.Model
{
    public class AudioRegion : INotifyPropertyChanged, IDisposable
    {
        public int UniqueID { get; set; }
        public int RegionIndex { get; set; }

        public AudioLayer ParentLayer { get; private set; }

        private bool _isSelected = false;
        public bool IsSelected
        {
            get { return (bool)_isSelected; }
            set
            {
                bool oldValue = _isSelected;
                _isSelected = value;
                if (oldValue != _isSelected)
                    OnPropertyChanged("IsSelected");
            }
        }

        public WaveStream AudioStream { get; set; }

        /// <summary>
        /// The current playback pointer to the audio data of this region. If it points inside the region, we'll be playing back. Otherwise we're skipped.
        /// When starting to play back, the pointer is set.
        /// While playing back, the pointer is incremented (always by SoundMixer._channelBufferSize).
        /// </summary>
        public long PlaybackPtr { get; set; }

        public MediaAudioIdent MediaIdent { get; set; }
        public string AudioFilePath
        {
            get
            {
                return MediaIdent.AudioFilePath;
            }
            set
            {
                MediaIdent.AudioFilePath = value;

                // we must reattach the audio stream
                ReattachAudioStream();

                OnPropertyChanged("AudioFilePath");
            }
        }


        double _timeOffset = 0;
        public double TimeOffset
        {
            get { return _timeOffset; }
            set
            {
                if (value < 0)
                    value = 0;
                _timeOffset = value;
                OnPropertyChanged("TimeOffset");
            }
        }

        public double TotalTime
        {
            get { return AudioStream.Duration; }
        }

        double _startTime = 0;
        public double StartTime
        {
            get { return _startTime; }
            set
            {
                if (value < 0)
                    value = 0;
                double oldValue = _startTime;
                _startTime = value;
                if (oldValue != _startTime)
                    OnPropertyChanged("StartTime");
            }
        }

        double _endTime = 0;
        public double EndTime
        {
            get { return _endTime; }
            set
            {
                if (AudioStream != null && value > AudioStream.Duration)
                    value = AudioStream.Duration;
                double oldValue = _endTime;
                _endTime = value;
                if (oldValue != _endTime)
                    OnPropertyChanged("EndTime");
            }
        }

        public double Duration
        {
            get
            {
                return EndTime - StartTime;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                PropertyChanged = null;
            }
        }

        public AudioRegion(AudioLayer parentLayer, double timeOffset, MediaAudioIdent mediaIdent, WaveStream audioStream)
        {
            UniqueID = MathHelper.UniqueID;
            ParentLayer = parentLayer;
            _timeOffset = timeOffset;
            MediaIdent = mediaIdent;
            AudioStream = audioStream;

            _startTime = 0;
            _endTime = (AudioStream != null) ? AudioStream.Duration : 0;
        }
        public AudioRegion(AudioLayer parentLayer, double timeOffset, MediaAudioIdent mediaIdent, WaveStream audioStream, double startTime, double endTime)
        {
            UniqueID = MathHelper.UniqueID;
            ParentLayer = parentLayer;
            _timeOffset = timeOffset;
            MediaIdent = mediaIdent;
            AudioStream = audioStream;

            _startTime = startTime;
            _endTime = (endTime < startTime) ? startTime : endTime;
            if (AudioStream != null && _endTime > AudioStream.Duration)
                _endTime = AudioStream.Duration;
        }

        public void ReattachAudioStream()
        {
            MediaAudio ma;
            foreach (MediaBase mb in LfsRecordController.project.MediaObjects)
            {
                if (mb.MediaType == MediaTypes.Audio)
                {
                    ma = (MediaAudio)mb;
                    if (ma.FilePath == MediaIdent.AudioFilePath)
                    {
                        //Debug.WriteLine("Reattaching audio stream");
                        AudioStream = ma.AudioStream;
                    }
                }
            }
        }

        public AudioRegionData DataStructure()
        {
            return new AudioRegionData()
            {
                UniqueID = UniqueID,
                RegionIndex = RegionIndex,
                IsSelected = IsSelected,
                MediaFilePath = MediaIdent.AudioFilePath,
                TimeOffset = TimeOffset,
                StartTime = StartTime,
                EndTime = EndTime
            };
        }
    }

    public class MediaAudioIdent
    {
        public string AudioFilePath { get; set; }
        public int MediaIndex { get; set; }
    }
}
